package cn.xutongxue.interview.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.xutongxue.interview.common.ResponseResult;
import cn.xutongxue.interview.enums.Errors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 全局异常补货
 * @author Administrator
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public ResponseResult<?> businessExceptionHandler(BusinessException e) {
        log.error("BusinessException", e);
        return ResponseResult.fail(e.getMessage(), e.getCode());
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseResult<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("RuntimeException", e);
        return ResponseResult.fail(e.getMessage());
    }

    @ExceptionHandler(NotRoleException.class)
    public ResponseResult<?> notRoleExceptionHandler(NotRoleException e) {
        log.error("NotRoleException", e);
        return ResponseResult.fail(Errors.NO_AUTH);
    }

    @ExceptionHandler(NotLoginException.class)
    public ResponseResult<?> notLoginExceptionHandler(NotLoginException e) {
        log.error("NotLoginException", e);
        return ResponseResult.fail("用户未登录", 400501);
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult<?> exceptionHandler(Exception e) {
        log.error("Exception", e);
        return ResponseResult.fail(Errors.UNKNOWN_ERROR);
    }

}
